home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / mus / play / DelfMPEG.lha / DelfMPEG / src / arexx.c next >
C/C++ Source or Header  |  2002-10-30  |  9KB  |  271 lines

  1. /*****************************************************************************
  2.  
  3.     DelfMPEG - MPEG audio player for Delfina DSP
  4.     Copyright (C) 1999-2002  Michael Henke
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. *****************************************************************************/
  21.  
  22.  
  23. #include <proto/exec.h>
  24. #include <proto/rexxsyslib.h>
  25. #include <proto/utility.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30.  
  31. /**
  32. *** in DelfMPEG.c
  33. **/
  34. extern ULONG verbose, ende, pause, havetag;
  35. extern LONG volume;
  36. extern UBYTE ID3v1_buffer[142];
  37. void initPCM(void);
  38. void setPosition(ULONG seconds);
  39.  
  40.  
  41. /**
  42. *** public variables
  43. **/
  44. struct Library *RexxSysBase;
  45. char *rexxfilename[2]={NULL,NULL};
  46. char *portname="DELFMPEG";
  47. WORD rexxstatus, rexxerror;
  48. char rexxfiletypebuf[512];
  49. ULONG rexxduration, rexxposition;
  50.  
  51.  
  52. static char rexxfilenamebuf[512];
  53. static char rexxreturnbuf[512];
  54. static struct MsgPort *rexxport=NULL;
  55. static WORD myport=0;
  56.  
  57.  
  58.  
  59.  
  60.  
  61. char *initRexx(void)
  62. {
  63.     if(!(RexxSysBase=OpenLibrary("rexxsyslib.library",36)))
  64.         return("unable to open rexxsyslib.library v36");
  65.  
  66.     if(FindPort(portname))
  67.         return("found another DelfMPEG already running");
  68.  
  69.     if(!(rexxport=CreateMsgPort()))
  70.         return("unable to create MsgPort");
  71.  
  72.     rexxport->mp_Node.ln_Pri=0;
  73.     rexxport->mp_Node.ln_Name=portname;
  74.     AddPort(rexxport);
  75.     if(!FindPort(portname))
  76.         return("AddPort() failed");
  77.     myport=1;
  78.  
  79.     rexxfilenamebuf[0]=0;
  80.  
  81.     return(NULL);   /* ok */
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. void cleanupRexx(void)
  89. {
  90.     struct MsgPort *mp;
  91.     struct Message *m;
  92.  
  93.     Forbid();
  94.     mp=FindPort(portname);
  95.     if(mp && myport)
  96.     {
  97.         while((m=GetMsg(mp))) ReplyMsg(m);
  98.         RemPort(mp); myport=0;
  99.     }
  100.     Permit();
  101.     if(rexxport) {DeleteMsgPort(rexxport); rexxport=NULL;}
  102.     if(RexxSysBase) {CloseLibrary(RexxSysBase); RexxSysBase=NULL;}
  103. }
  104.  
  105.  
  106. static void print0(char *s)
  107. {
  108.     if(verbose) printf("  command '%s'\n",s);
  109. }
  110.  
  111. static void retstr(struct RexxMsg *rm, char *comm, char *res)
  112. {
  113.     if(verbose) printf("  command '%s' returns '%s'\n",comm,res);
  114.     if(rm->rm_Action&RXFF_RESULT)   /* result string expected? */
  115.     {
  116.         rm->rm_Result1=0;
  117.         rm->rm_Result2=(LONG)CreateArgstring(res,(ULONG)strlen(res));
  118.     }
  119. }
  120.  
  121. void handleRexx(void)
  122. {
  123.     struct RexxMsg *rm;
  124.     char *arg0, *substr;
  125.     while((rm=(struct RexxMsg*)GetMsg(rexxport)))
  126.     {
  127.         if(IsRexxMsg(rm))
  128.         {
  129.             if((rm->rm_Action&RXCODEMASK)==RXCOMM)  /* it's a command */
  130.             {
  131.                 arg0=ARG0(rm);
  132.                 if(Stricmp(arg0,"QUIT")==0)
  133.                 {
  134.                     print0(arg0);
  135.                     ende=2;
  136.                     rexxstatus=0;   /* stop */
  137.                 } else
  138.                 if(Stricmp(arg0,"STOP")==0)
  139.                 {
  140.                     print0(arg0);
  141.                     ende=1;
  142.                     rexxstatus=0;   /* stop */
  143.                 } else
  144.                 if(Stricmp(arg0,"PAUSE")==0)
  145.                 {
  146.                     print0(arg0);
  147.                     pause=2;
  148.                     if(rexxstatus==1) rexxstatus=2; /* play -> pause */
  149.                 } else
  150.                 if(Stricmp(arg0,"CONTINUE")==0)
  151.                 {
  152.                     print0(arg0);
  153.                     pause=0;
  154.                     if(rexxstatus==2) rexxstatus=1; /* pause -> play */
  155.                 } else
  156.                 if(Strnicmp(arg0,"PLAY",4)==0)
  157.                 {
  158.                     substr=arg0+4;
  159.                     while((*substr)==' ') substr++;
  160.                     if((*substr)==0) printf("**command '%s' - missing parameter <file>\n",arg0);
  161.                     else
  162.                     {
  163.                         print0(arg0);
  164.                         strncpy(rexxfilenamebuf,substr,sizeof(rexxfilenamebuf));
  165.                         rexxfilename[0]=rexxfilenamebuf;
  166.                     }
  167.                 } else
  168.                 if(Strnicmp(arg0,"SETVOLUME",9)==0)
  169.                 {
  170.                     substr=arg0+9;
  171.                     while((*substr)==' ') substr++;
  172.                     if((*substr)==0) printf("**command '%s' - missing parameter <volume>\n",arg0);
  173.                     else
  174.                     {
  175.                         LONG v=atol(substr);
  176.                         if((v<0) || (v>200))
  177.                             printf("**command '%s' - invalid parameter value\n",arg0);
  178.                         else
  179.                         {
  180.                             print0(arg0);
  181.                             volume=v; initPCM();
  182.                         }
  183.                     }
  184.                 } else
  185.                 if(Strnicmp(arg0,"SETPOSITION",11)==0)
  186.                 {
  187.                     substr=arg0+11;
  188.                     while((*substr)==' ') substr++;
  189.                     if((*substr)==0) printf("**command '%s' - missing parameter <seconds>\n",arg0);
  190.                     else
  191.                     {
  192.                         LONG pos=(ULONG)atol(substr);
  193.                         if(rexxstatus==0)
  194.                             printf("**command '%s' - not allowed in STOP mode\n",arg0);
  195.                         else
  196.                         {
  197.                             print0(arg0);
  198.                             if(pos<0) pos=0;
  199.                             setPosition((ULONG)pos);
  200.                         }
  201.                     }
  202.                 } else
  203.                 if(Stricmp(arg0,"GETSTATUS")==0)
  204.                 {
  205.                     sprintf(rexxreturnbuf,"%d",rexxerror?rexxerror:rexxstatus);
  206.                     retstr(rm,arg0,rexxreturnbuf);
  207.                 } else
  208.                 if(Stricmp(arg0,"GETFILENAME")==0)
  209.                 {
  210.                     strcpy(rexxreturnbuf,rexxfilenamebuf);
  211.                     retstr(rm,arg0,rexxreturnbuf);
  212.                 } else
  213.                 if(Stricmp(arg0,"GETFILETYPE")==0)
  214.                 {
  215.                     strcpy(rexxreturnbuf,rexxfiletypebuf);
  216.                     retstr(rm,arg0,rexxreturnbuf);
  217.                 } else
  218.                 if(Stricmp(arg0,"GETDURATION")==0)
  219.                 {
  220.                     sprintf(rexxreturnbuf,"%ld",rexxduration);
  221.                     retstr(rm,arg0,rexxreturnbuf);
  222.                 } else
  223.                 if(Stricmp(arg0,"GETPOSITION")==0)
  224.                 {
  225.                     sprintf(rexxreturnbuf,"%ld",rexxposition);
  226.                     retstr(rm,arg0,rexxreturnbuf);
  227.                 } else
  228.                 if(Stricmp(arg0,"GETTITLE")==0)
  229.                 {
  230.                     if(!havetag) rexxreturnbuf[0]=0;
  231.                     else strcpy(rexxreturnbuf,&ID3v1_buffer[00]);
  232.                     retstr(rm,arg0,rexxreturnbuf);
  233.                 } else
  234.                 if(Stricmp(arg0,"GETARTIST")==0)
  235.                 {
  236.                     if(!havetag) rexxreturnbuf[0]=0;
  237.                     else strcpy(rexxreturnbuf,&ID3v1_buffer[31]);
  238.                     retstr(rm,arg0,rexxreturnbuf);
  239.                 } else
  240.                 if(Stricmp(arg0,"GETALBUM")==0)
  241.                 {
  242.                     if(!havetag) rexxreturnbuf[0]=0;
  243.                     else strcpy(rexxreturnbuf,&ID3v1_buffer[62]);
  244.                     retstr(rm,arg0,rexxreturnbuf);
  245.                 } else
  246.                 if(Stricmp(arg0,"GETCOMMENT")==0)
  247.                 {
  248.                     if(!havetag) rexxreturnbuf[0]=0;
  249.                     else strcpy(rexxreturnbuf,&ID3v1_buffer[98]);
  250.                     retstr(rm,arg0,rexxreturnbuf);
  251.                 } else
  252.                 if(Stricmp(arg0,"GETYEAR")==0)
  253.                 {
  254.                     if(!havetag) rexxreturnbuf[0]=0;
  255.                     else strcpy(rexxreturnbuf,&ID3v1_buffer[93]);
  256.                     retstr(rm,arg0,rexxreturnbuf);
  257.                 } else
  258.                 if(Stricmp(arg0,"GETGENRE")==0)
  259.                 {
  260.                     if(!havetag) rexxreturnbuf[0]=0;
  261.                     else sprintf(rexxreturnbuf,"%d",(int)ID3v1_buffer[141]);
  262.                     retstr(rm,arg0,rexxreturnbuf);
  263.                 } else
  264.                 printf("**unknown command '%s'\n",arg0);
  265.             }
  266.         }
  267.         ReplyMsg((struct Message*)rm);
  268.     }
  269. }
  270.  
  271.